package com.own.component.util.locate.model;

import com.own.component.util.locate.util.LocateDistanceUtil;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;

/**
 * LocateModel
 *
 * @author chenxueli
 * @date 2023-08-01 08:37:57
 */
@Getter
@NoArgsConstructor
public class LocateModel implements Serializable {

    public static final LocateModel NONE = new LocateModel(false);

    /**
     * 纬度
     */
    private BigDecimal latitude;

    /**
     * 经度
     */
    private BigDecimal longitude;

    /**
     * 是否是有效的
     */
    private boolean available;

    public LocateModel(BigDecimal latitude, BigDecimal longitude) {
        this.latitude = latitude;
        this.longitude = longitude;
        this.available = latitude.compareTo(BigDecimal.ZERO) != 0 && longitude.compareTo(BigDecimal.ZERO) != 0;
    }

    public LocateModel(String latitude, String longitude) {
        this(
                StringUtils.hasText(latitude) ? new BigDecimal(latitude) : BigDecimal.ZERO,
                StringUtils.hasText(longitude) ? new BigDecimal(longitude) : BigDecimal.ZERO
        );
    }

    public LocateModel(Double latitude, Double longitude) {
        this(
                latitude == null ? BigDecimal.ZERO : new BigDecimal(latitude),
                longitude == null ? BigDecimal.ZERO : new BigDecimal(longitude)
        );
    }

    private LocateModel(boolean available) {
        this.available = available;
        this.latitude = BigDecimal.ZERO;
        this.longitude = BigDecimal.ZERO;
    }

    public BigDecimal calc(LocateModel locate) {
        return LocateDistanceUtil.calc(this, locate);
    }

    public BigDecimal calc(String latitude, String longitude) {
        return LocateDistanceUtil.calc(this, new LocateModel(latitude, longitude));
    }

    public BigDecimal calc(BigDecimal latitude, BigDecimal longitude) {
        return LocateDistanceUtil.calc(this, new LocateModel(latitude, longitude));
    }

    public BigDecimal calc(Double latitude, Double longitude) {
        return LocateDistanceUtil.calc(this, new LocateModel(latitude, longitude));
    }

}
